import { Popup } from 'devextreme-react';
import React, { useEffect, useImperativeHandle, useRef, useState } from 'react';
import { intl } from "@/utils";
import Form, { Item } from 'devextreme-react/form';
import { getCpasByCategory, gettableIsVerifyTid, saveCpasDescList } from '../service';
import { useModel } from 'umi';
import notify from 'devextreme/ui/notify';

const FullNamePupop = React.forwardRef((props: any, ref: any) => {
    const { initialState } = useModel('@@initialState');
    const { dbname } = initialState?.curPart as API.CurPart;
    const [visible, setVisible] = useState<boolean>(false);
    const cpasTableTid = useRef<any>(''); // tid

    const refsForm = useRef<any>();
    const [formAllData, setFormAllData] = useState<any>({ shortName: "", fullName: "" });


    const onCancel = () => {
        setVisible(false);
        setFormAllData({ shortName: "", fullName: "" });
        cpasTableTid.current = '';
    }
    const onOk = async () => {
        let cpasDescTextData = [{
            tid: cpasTableTid.current,
            category: "aliasName",
            describe: JSON.stringify(formAllData.fullName)
        }];
        const tempCpasDesc = {
            dbname,
            cpasDesc: cpasDescTextData
        }
        const res = await saveCpasDescList(tempCpasDesc);
        if (res.success) {
            notify(`全称编辑成功！`, "success", 2000);
            onCancel();
        } else {
            notify(`全称编辑失败！`, "error", 2000);
        }
    }



    // 自定义方法
    useImperativeHandle(ref, () => ({
        showPupop: async (tableTitle: string, shortName: string) => {
            const resTid = await gettableIsVerifyTid({
                tableName: tableTitle,
                dbname,
                isTid: true
            });
            if (resTid.success) {
                console.log(resTid.data);
                const { data } = resTid;
                cpasTableTid.current = data[0].tid;
                console.log(data, '@@@');
                if (data.length) {
                    const resName = await getCpasByCategory({
                        dbname,
                        tid: [data[0].tid],
                        category: "aliasName",
                    });
                    let tempText = '';
                    if (resName.success) {
                        if (resName.data.length) {
                            tempText = JSON.parse(resName.data[0].describe)
                        }
                        setFormAllData({ shortName, fullName: tempText });
                        setVisible(true);
                        return;
                    }
                    return notify(`获取全称失败！`, "error", 2000);
                }
            }
            return notify(`获取tid失败！`, "error", 2000);
        },
    }));



    return (
        <>
            <Popup
                visible={visible}
                title='表名全称编辑'
                width={400}
                height={300}
                // dragOutsideBoundary={true}
                // resizeEnabled={true}
                onHiding={() => {
                    onCancel()
                }}
                toolbarItems={[
                    {
                        widget: 'dxButton',
                        toolbar: 'bottom',
                        location: 'after',
                        options: {
                            text: intl('pub.cancel', '取消'),
                            onClick: () => onCancel()
                        },
                    },
                    {
                        widget: 'dxButton',
                        toolbar: 'bottom',
                        location: 'after',
                        options: {
                            text: intl('pub.ok', '确定'),
                            type: 'default',
                            onClick: () => onOk()
                        },
                    },

                ]}
                contentRender={() => (
                    <div>
                        <Form
                            ref={refsForm}
                            formData={formAllData}
                            colCount={2}
                            labelLocation={"left"}
                            labelMode={"outside"}
                            focusStateEnabled={true}
                            showColonAfterLabel={true}
                            alignItemLabels={true}
                        >
                            <Item colSpan={2} dataField="shortName" label={{ text: '简称' }}
                                editorOptions={{
                                    readOnly: true
                                }} />
                            <Item colSpan={2} dataField="fullName" label={{ text: '全称' }}
                                editorOptions={{
                                    maxLength: 100
                                }} />
                        </Form>
                        <div style={{ margin: "8px 0 0 15px", color: "red" }}>注：“全称”用于导出Excel 具体Sheet页签名称</div>
                    </div>
                )}
            />
        </>
    );
});

export default FullNamePupop;
